package com.jm.programming.stream;

import com.jm.programming.stream.pojo.Author;
import com.jm.programming.stream.utils.CreateDataUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @date 2022/7/6 7:49
 */
public class StreamMidMain {
    public static void main(String[] args) {
        flatMap2();
    }

    /**
     * 演示流中filter，输出所有大于十八岁的作家名字。
     */
    static void filter(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .filter(author -> author.getAge() < 18)
                .forEach(author -> System.out.println(author.getName()));
    }

    /**
     * 演示流中map，获取作家的名称并转为String后打印
     */
    static void map(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .map(author -> author.getName())
                .forEach(author -> System.out.println(author + "_" + author.getClass().getName()) );
    }

    /**
     * 使用map将所有的作家年龄加10，并输出
     */
    static void mapOfCount(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .map(author -> author.getAge())
                .map(age -> age + 10)
                .forEach(age -> System.out.println(age + "_" + age.getClass().getName()) );

    }

    /**
     * 使用distinct去重打印打印作家信息
     */
    static void distinct(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .distinct()
                .forEach(author -> System.out.println(author));
    }

    /**
     * 空参 sorted() ：
     * 按年龄进行降序排序，且去重
     * 被排序的元素需要实现compare接口
     */
    static void sorted(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .distinct()
                .sorted()
                .forEach(author -> System.out.println(author.getAge()));
    }

    /**
     * 有参sorted
     */
    static void sorted2(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .distinct()
                .sorted((a1,a2) -> a2.getAge() - a1.getAge())
                .forEach(author -> System.out.println(author.getAge()));
    }

    /**
     * limit
     */
    static void limit(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .distinct()
                .sorted((a1,a2) -> a2.getAge() - a1.getAge())
                .limit(2)
                .forEach(author -> System.out.println(author.getAge()));
    }

    /**
     * 使用skip打印流中除了最大年另外的其它作家，重复，且按年龄降序
     */
    static void skip(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .distinct()
                .sorted((a1,a2) -> a2.getAge() - a1.getAge())
                .skip(1)
                .forEach(author -> System.out.println(author.getAge()));
    }

    /**
     * 打印当前图书所有数据，且去重
     */
    static void flatMap(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .forEach(book -> System.out.println(book.getName()));
    }

    /**
     * 打印图书的所有分类，且去重，不能以逗号分割。
     */
    static void flatMap2(){
        List<Author> authors = CreateDataUtil.getAuthors();
        authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(category -> System.out.println(category));
    }
}
